package com.ljkj.cordial.chat.presenter.response;

import android.app.Activity;

import com.lanjiu.lib.imclient.ims.IMSRequester;
import com.lanjiu.lib.utils.CollectionUtils;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.pro.business.BusinessProtocolMessages;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.dao.notify.ChatContentTable;
import com.ljkj.cordial.chat.dao.notify.GroupMemberTable;
import com.ljkj.cordial.chat.dao.notify.GroupTable;
import com.ljkj.cordial.chat.notify.NotificationUtils;

/**
 * 邀请
 */
public class GroupMemberAddProtocolResponse extends AResponseBusinessPresenter {
    private Activity activity;

    @Override
    public void onResponse(Activity activity, boolean isOnScreen, String msgType, Object msg) {
        this.activity = activity;
        BusinessProtocolMessages.GroupMemberAddProtocol response = (BusinessProtocolMessages.GroupMemberAddProtocol) msg;
//        String userType = response.getUserType();
        if (response != null && !CollectionUtils.isEmpty(response.getAllGroupMemberVoList())) {
            // 同意申请 和 邀请入群 没办法区分，先不弹通知了
//            NotificationUtils.messageNotify(activity);
            BusinessProtocolEntities.AllGroupMemberVO groupMemberVO = response.getAllGroupMemberVoList().get(0);
            String groupId = null;
            if (groupMemberVO.getGuestGroupId() != 0)
                groupId = String.valueOf(groupMemberVO.getGuestGroupId());
            if (groupMemberVO.getRegisteredGroupId() != 0)
                groupId = String.valueOf(groupMemberVO.getRegisteredGroupId());
            if (null != groupId)
                IMSRequester.requestGroupMemberList(groupId);
        }
//        GroupTable groupTable = null;
//        if (response.getAllGroupMemberVoList() != null && !response.getAllGroupMemberVoList().isEmpty()) {
//            List<BusinessProtocolEntities.AllGroupMemberVO> list = response.getAllGroupMemberVoList();
//            for (BusinessProtocolEntities.AllGroupMemberVO group : list) {
//                if (IMSContacts.UserType.REGISTERED_USER.equals(userType)) {
//                    groupTable = GroupTable.queryRowByGroupIdNotNull(String.valueOf(group.getRegisteredGroupId()));
//                    groupTable.setGroupType(userType);
//                    groupTable.setGroupId(String.valueOf(group.getRegisteredGroupId()));
//                    groupTable.setGroupName("注册群" + group.getRegisteredGroupId());
////                    groupTable.setGroupOwnerId(group.getGroupOwner());
////                    groupTable.setGroupAvatar(group.getUserPhoto());
//                    groupTable.setAnnouncementContent(group.getAnnouncementContent());
//                    groupTable.setAnnouncementTitle(group.getAnnouncementTitle());
//                } else {
//                    groupTable = GroupTable.queryRowByGroupIdNotNull(String.valueOf(group.getGuestGroupId()));
//                    groupTable.setGroupType(userType);
//                    groupTable.setGroupId(String.valueOf(group.getGuestGroupId()));
//                    groupTable.setGroupName("游客群" + group.getGuestGroupId());
////                    groupTable.setGroupOwnerId(group.getGroupOwner());
////                    groupTable.setGroupAvatar(group.getUserPhoto());
//                    groupTable.setAnnouncementContent(group.getAnnouncementContent());
//                    groupTable.setAnnouncementTitle(group.getAnnouncementTitle());
//                }
//
//            }
//            if (groupTable != null) {
//                insert(groupTable);
//            }
//        }
//        GroupMemberTable groupMemberTable = null;
//        if (IMSContacts.MsgType.GROUP_MEMBER_APPLY_AGREE_REGISTERED.equals(msgType)) {
//            if (response.hasRegisteredGroupMember()) {
//                BusinessProtocolEntities.RegisteredGroupMember member = response.getRegisteredGroupMember();
//                if (member.hasGroup()) {
//                    groupTable = getRegisteredGroup(member.getGroup());
//                    groupMemberTable = getGroupMemberTable(member.getGroupMemberId(), member.getGroupMemberName(), member.getGroupMemberNick(), member.getGroupMemberUserId(), member.getGroupMemberAlias(), member.getOnlineStatus(), IMSContacts.UserType.REGISTERED_USER, member.getGroupMemberAvatar(), member.getGroupMemberType(), groupTable.getGroupId(), groupTable.getGroupName(), groupTable.getGroupType());
//                    if (TextUtils.isEmpty(groupTable.getGroupOwnerId())) {
//                        groupTable.setGroupOwnerId(GroupMemberTable.OWNER == member.getGroupMemberType() ? member.getGroupMemberUserId() : "");
//                    }
//
//                }
//            }
//        } else if (IMSContacts.MsgType.GROUP_MEMBER_APPLY_AGREE_TOURISTS.equals(msgType)) {
//            if (response.hasTouristsGroupMember()) {
//                BusinessProtocolEntities.TouristsGroupMember member = response.getTouristsGroupMember();
//                if (member.hasGroup()) {
//                    groupTable = getTouristsGroup(member.getGroup());
//                    groupMemberTable = getGroupMemberTable(member.getGroupMemberId(), member.getGroupMemberName(), member.getGroupMemberNick(), member.getGroupMemberUserId(), member.getGroupMemberAlias(), member.getOnlineStatus(), IMSContacts.UserType.TOURISTS_USER, member.getGroupMemberAvatar(), member.getGroupMemberType(), groupTable.getGroupId(), groupTable.getGroupName(), groupTable.getGroupType());
//                    if (TextUtils.isEmpty(groupTable.getGroupOwnerId())) {
//                        groupTable.setGroupOwnerId(GroupMemberTable.OWNER == member.getGroupMemberType() ? member.getGroupMemberUserId() : "");
//                    }
//                }
//            }
//        }
//        if (groupTable == null || groupMemberTable == null) {
//            return;
//        }
//        insert(groupTable, groupMemberTable);
        if (isOnScreen) {
            refresh(activity);
        }
    }

//
//    private GroupTable getRegisteredGroup(BusinessProtocolEntities.RegisteredGroup group) {
//        GroupTable groupTable = GroupTable.queryRowByGroupIdNotNull(group.getGroupId());
//        groupTable.setGroupName(group.getGroupName());
//        groupTable.setGroupId(group.getGroupId());
//        groupTable.setGroupType(IMSContacts.UserType.REGISTERED_USER);
//        if (group.hasGroupAnnouncements()) {
//            BusinessProtocolEntities.GroupAnnouncements groupS = group.getGroupAnnouncements();
//            groupTable.setAnnouncementContent(groupS.getAnnouncementContent());
//            groupTable.setAnnouncementTitle(groupS.getAnnouncementTitle());
//            groupTable.setAnnouncementId(groupS.getAnnouncementId());
//        }
//        if (group.hasSettings()) {
//            BusinessProtocolEntities.Settings set = group.getSettings();
//            groupTable.setSnapchatStatus(set.getSnapchatStatus());
//            groupTable.setSnapchatTimeLength(set.getSnapchatTimeLength());
//            groupTable.setSnapchatTimeUnit(set.getSnapchatTimeUnit());
//        }
//        return groupTable;
//    }
//
//    private GroupTable getTouristsGroup(BusinessProtocolEntities.TouristsGroup group) {
//        GroupTable groupTable = GroupTable.queryRowByGroupIdNotNull(group.getGroupId());
//
//        groupTable.setGroupName(group.getGroupName());
//        groupTable.setGroupId(group.getGroupId());
//        groupTable.setGroupType(IMSContacts.UserType.TOURISTS_USER);
//        groupTable.setGroupOwnerId(group.getGroupOwnerUserId());
//        groupTable.setGroupAvatar(group.getGroupAvatar());
//        if (group.hasGroupAnnouncements()) {
//            BusinessProtocolEntities.GroupAnnouncements groupS = group.getGroupAnnouncements();
//            groupTable.setAnnouncementContent(groupS.getAnnouncementContent());
//            groupTable.setAnnouncementTitle(groupS.getAnnouncementTitle());
//            groupTable.setAnnouncementId(groupS.getAnnouncementId());
//        }
//        if (group.hasSettings()) {
//            BusinessProtocolEntities.Settings set = group.getSettings();
//            groupTable.setSnapchatStatus(set.getSnapchatStatus());
//            groupTable.setSnapchatTimeLength(set.getSnapchatTimeLength());
//            groupTable.setSnapchatTimeUnit(set.getSnapchatTimeUnit());
//        }
//        return groupTable;
//    }
//
//    private GroupMemberTable getGroupMemberTable(String groupMemberId, String groupMemberName, String groupMemberNick, String groupMemberUserId, String groupMemberAlias, String onlineStatus, String userType, String groupMemberAvatar, int groupMemberType, String groupId, String groupName, String groupType) {
//        GroupMemberTable groupMemberTable = GroupMemberTable.create(groupId, groupMemberUserId);
//        groupMemberTable.setGroupMemberId(groupMemberId);
//        groupMemberTable.setGroupMemberName(groupMemberName);
//        groupMemberTable.setGroupMemberNick(groupMemberNick);
//        groupMemberTable.setGroupMemberUserId(groupMemberUserId);
//        groupMemberTable.setGroupMemberAlias(groupMemberAlias);
//        groupMemberTable.setOnlineStatus(onlineStatus);
//        groupMemberTable.setUserType(IMSUtils.userTypeAnalysis(groupMemberUserId));
//        groupMemberTable.setGroupMemberAvatar(groupMemberAvatar);
//        groupMemberTable.setGroupMemberType(groupMemberType);
//        groupMemberTable.setGroupId(groupId);
//        groupMemberTable.setGroupName(groupName);
//        groupMemberTable.setGroupType(groupType);
//        return groupMemberTable;
//    }

    @Override
    public void insert(Object... table) {
        super.insert(table);
        try {
            GroupTable groupTable = (GroupTable) table[0];
//            GroupMemberTable groupMemberTable = (GroupMemberTable) table[1];
            GroupTable.insert(groupTable);
//            GroupMemberTable.insertOrUpdate(groupMemberTable);

            ChatContentTable cct = ChatContentTable.queryRowByTargetIdNotNull(groupTable.getGroupId(), Canstants.GROUP_CHAT);
            cct.setIsTOP(0);
            cct.setChatType(Canstants.GROUP_CHAT);
            cct.setTargetId(groupTable.getGroupId());
            cct.setTargetName(groupTable.getGroupName());
            cct.setTargetType(groupTable.getGroupType());
            cct.setUpdateTime(System.currentTimeMillis());

            String[] v = GroupMemberTable.getAvatarEntityLoc(groupTable.getGroupId());
            if (v != null) {
                cct.setTargetMemberUserId(v[0]);
                cct.setTargetAvatarUrl(v[1]);
                cct.setTargetAvatarPath(v[2]);
            }
            cct.setLastMsgType(MsgType.Text);
            cct.setLastMsg(String.format(ChatApp.getInstance().getResources().getString(R.string.Apply_Group), cct.getTargetName()));
            cct.setIsRead(0);
            ChatContentTable.insert(cct);
            if (activity != null && !activity.isFinishing()) {
                activity.runOnUiThread(() -> {
                    //加载成员
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
